package cn.edu.ctbu.test.api;


import cn.edu.ctbu.test.core.PageUtils;
import cn.edu.ctbu.test.domain.Sc;
import cn.edu.ctbu.test.domain.Score;
import cn.edu.ctbu.test.service.ScoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController // RestController只能返回数据
@NoRepositoryBean
@RequestMapping("/api/score")
public class ScoreRestController {
    @Autowired
    private ScoreService scoreService;

    @GetMapping("/list")
    public List<Score> getAll(){
        List<Score> scores=scoreService.findAll();

        return  scores;
    }

    @GetMapping("/getbypageScore")
    public PageUtils getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size,
                               @RequestParam(value = "condition",defaultValue = "") Long condition){

        //按id排倒序
        Sort sort= Sort.by(Sort.Direction.ASC,"id");

        Page<Score> scorePage;

        if(ObjectUtils.isEmpty(condition)){
            Pageable pageable = PageRequest.of(page,size,sort);
            scorePage= scoreService.findAll(pageable);
        }else {
            Score score= new Score();
            score.setStudentId(condition);

            Pageable pageable=PageRequest.of(0,10,sort);

            ExampleMatcher matcher= ExampleMatcher.matching()
                    .withMatcher("condition",ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Score> example=Example.of(score,matcher);

            scorePage=scoreService.findAll(example,pageable);
        }


//        Pageable pageable= PageRequest.of(page,size,sort);
//        Page<Score> scorePage=scoreService.findAll(pageable);

        PageUtils pageUtils=new PageUtils(scorePage.getContent(),scorePage.getTotalElements());

        return pageUtils;

    }

    @GetMapping("/get/{id}")
    public Score get(@PathVariable Long id){

        Score score=scoreService.getById(id);

        return  score;//把id返回到客户端
    }


    /**
     * 以下为增删改查
     * @param
     * @return
     */
    @GetMapping("/findByStudentId")
    public List<Score> findByStudentId(Long studentId){
        List<Score> scores=scoreService.findByStudentId(studentId);
        return scores;
    }

    @PostMapping("/insert") //添加人员不用返回人员，返回成功或者失败就可以了
    public Score insert(Score score){
        Score score1=scoreService.insert(score);

        return score1;
    }

    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Long id){

        scoreService.delete(id);
    }

    @PutMapping("/update")
    public Score update(Score score){

        //读取旧成绩数据
        Score oldscore=scoreService.getById(score.getStudentId());

        score=scoreService.update(oldscore);

        return score;
    }
    
}
